package com.beehoo.dcportal.common;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.util.Map;

import com.beehoo.dcportal.common.base.BaseAction;
import com.opensymphony.xwork2.ActionContext;

public class ValidateImgAction extends BaseAction {

	private static final long serialVersionUID = -7507446824926724835L;
	private ValidateImgService validateImgService;
	private ByteArrayInputStream inputStream;

	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	public void setValidateImgService(ValidateImgService validateImgService) {
		this.validateImgService = validateImgService;
	}

	public String execute() {
		int width = 65;
		int height = 20;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		Map<String, Object> randMap = validateImgService.createValidateImg(
				image, width, height);

		ByteArrayInputStream input = (ByteArrayInputStream) randMap.get("img");
		String rand = (String) randMap.get("str");

		Map<String, Object> session = ActionContext.getContext().getSession();
		session.put(Constant.SESSION_VALIDATECODE, rand);
		this.setInputStream(input);

		return SUCCESS;
	}
}
